import akka.actor.{Actor, ActorSystem, Props, ActorRef}
import scala.io.StdIn
import scala.util.Random

case object Start
case object Stop
case object Ping
case object Pong


case class PingActor(pong: ActorRef) extends Actor {
    def receive = {
        case Start => { 
            println("Show On")
            pong ! Ping
        }
        case Pong => {
            println("pong...") 
            sender ! Ping
        }
        case Stop => println("is over")
    }
}

object PingActor {
    def props(pong: ActorRef) = Props(new PingActor(pong))
}

class PongActor extends Actor {
    def receive = {
        case Ping => {
            if(Random.nextInt(10) < 4) {
                sender ! Stop
            } else {
                println("ping...")
                sender ! Pong
            }
        }
    }
}

object PongActor {
    def props = Props[PongActor]
}

object PingPong extends App {
    val system = ActorSystem("helloakka")
    try {
        val pong = system.actorOf(PongActor.props, "Pong")
        val ping = system.actorOf(PingActor.props(pong), "Ping")        
        ping ! Start
        StdIn.readLine()
    } finally {
        system.terminate()
    }
 
}
